package ipset

import (
	"k8s.io/klog"
	"time"
)

const IPSetName = "hcvip"

func InitVIPSet() *IPSet {
	for {
		mcastdst, err := New(IPSetName, "hash:net", Params{})
		if err != nil {
			klog.Infof("err is %v", err)
		}
		if err != nil {
			klog.Errorf("error add  ipset set  hcvip  %err", err)
			time.Sleep(time.Second)
		} else {
			return mcastdst
		}
	}
	return nil
}

func (mcastdst *IPSet) RefreshIPSet(setList []string) error {
	for _, ip := range setList {
		err := mcastdst.Add(ip, 0)
		if err != nil {
			klog.Errorf("mcastdst add %v err %v", ip, err)
		}
	}

	return nil
}

func (mcastdst *IPSet) DelIPSet(setList []string) error {
	for _, ip := range setList {
		err := mcastdst.Del(ip)
		if err != nil {
			klog.Errorf("mcastdst del %v err %v", ip, err)
		}
	}
	return nil
}

func (mcastdst *IPSet) DestroyIPSet() error {
	err := mcastdst.Destroy()
	if err != nil {
		klog.Errorf("mcastdst destroy hcvip err %v", err)
	}
	return nil
}
